import { world } from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { EntityTypeIds } from "@sapi-game/utils/vanila-data";
import { KcqdMainState } from "../mainState";

export class kcqdSnatch extends GameComponent<KcqdMainState> {
    override onAttach(): void {
        this.subscribe(world.afterEvents.entityHitEntity, (t) => {
            if (
                t.damagingEntity.typeId == EntityTypeIds.Player &&
                t.hitEntity.typeId == EntityTypeIds.Player
            ) {
                const attacker = this.context.players.getById(
                    t.damagingEntity.id
                );
                const victim = this.context.players.getById(t.hitEntity.id);
                if (!attacker || !victim) return;
                if (victim.isSausage) {
                    victim.isSausage = false;
                    attacker.isSausage = true;
                    //清除
                    attacker.clear(this.state.sausageItem.typeId);
                    victim.clear(this.state.sausageItem.typeId);
                    //给烤肠
                    this.state.giveSausage(attacker.player!);
                    //消息
                    attacker.title("§c§l你是烤肠", "§b§l保护好你的烤肠");
                    this.context.players.sendMessage(
                        `§c§l${attacker.name}§6抢走了§a${victim.name}§6的烤肠`
                    );
                }
            }
        });
    }
}
